#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Date    : 2020-03-14 20:53:55
# @Author  : 胡宗尧 (1448424184@qq.com)
# @Link    : https://www.cnblogs.com
# @Version : $Id$

# coding: utf-8
print("importing packages...")
import sys
import os
sys.path.append(os.pardir)
import pickle
import numpy as np
from emnist_show import *
from emnist import load_emnist
from PIL import Image
from random import randint

d = {0: 48, 1: 49, 2: 50, 3: 51, 4: 52, 5: 53, 6: 54, 7: 55, 8: 56, 9: 57, 10: 65, 11: 66,
     12: 67, 13: 68, 14: 69, 15: 70, 16: 71, 17: 72, 18: 73, 19: 74, 20: 75, 21: 76,
     22: 77, 23: 78, 24: 79, 25: 80, 26: 81, 27: 82, 28: 83, 29: 84, 30: 85, 31: 86,
     32: 87, 33: 88, 34: 89, 35: 90, 36: 97, 37: 98, 38: 100, 39: 101, 40: 102, 41: 103,
     42: 104, 43: 110, 44: 113, 45: 114, 46: 116}


def img_show(img):
    pil_img = Image.fromarray(np.uint8(img)).transpose(
        Image.FLIP_LEFT_RIGHT).rotate(90)
    pil_img.show()


print("loading emnist1... ")
(x_train1, t_train1), (_, _) = load_emnist(normalize=False)
print("loading emnist2... ")
(x_train2, t_train2), (_, _) = load_emnist(one_hot_label=True)

index = 10  # randint(0, x_train1.shape[0])

with open("weight.pkl", 'rb') as f:
    print("loading files...")
    network = pickle.load(f)

img = x_train1[index]
label = t_train1[index]

print("计算中...")
result = list(network.predict(x_train2[index]))
l = ["概率由高到低排列为："]

for i in range(5):
    del_index = np.argmax(result)
    result.pop(del_index)
    l.append(str(i + 1) + ":" + str(chr(d[del_index])))


print("神经网络输出结果为:", l)
print("答案为:", chr(d[label]))

img = img.reshape(28, 28)  # 把图像的形状变为原来的尺寸

img_show(img)
